package com.poker.duiued;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.poker.iuious.yhyhjs.jklimj.User;
import com.poker.jkiuus.R;
import com.poker.jkiuus.uyrfgd.AppManager;
import com.poker.jkiuus.uyrfgd.DatabaseHelper;


public class SettingsActivity extends AppCompatActivity {
    private TableRow more_page_row3;
    private TableRow more_page_row1;
    private TableRow more_page_row6;
    private TableRow more_page_row7;
    private TableRow more_page_row5;
    private TableRow more_page_row4;
    private String userName;
    private Toast currentToast;
    private LinearLayout content;
    private TextView txtUserName;
    private TextView txtUserID;
    private TextView txtLastTime;
    private TextView tvPrivacyPolicy;
    private TextView tvTermsConditions;
    private DatabaseHelper databaseHelper;
    private String privacyPolicyText = "<u>Privacy Agreement</u>";
    private String termsConditionsText = "<u>Terms & Conditions</u>";
    private User user = new User();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        AppManager.addActivity(this);

        content = findViewById(R.id.content);
        txtUserName = findViewById(R.id.txtUserName);
        txtUserID = findViewById(R.id.txtUserID);
        txtLastTime = findViewById(R.id.txtLastTime);
        databaseHelper = new DatabaseHelper(this);

        if (getIntent().hasExtra("USER_NAME")) {
            userName = getIntent().getStringExtra("USER_NAME");
            if (!userName.isEmpty()) {
                content.setVisibility(View.VISIBLE);
                user = databaseHelper.queryUserByNameOrEmail(userName);
                txtUserName.setText(user.getName());
                txtUserID.setText(String.valueOf(user.getLoginId()));
                txtLastTime.setText(user.getLoginTime());
            } else {
                content.setVisibility(View.GONE);
            }
        } else {
            userName = "";
            content.setVisibility(View.GONE);
        }

        more_page_row1 = findViewById(R.id.more_page_row1);
        more_page_row1.setOnClickListener(v -> {
            if ("".equals(userName)){
                showRegistrationAlertDialog();
            }else {
                Intent intent = new Intent(SettingsActivity.this, UserAccountActivity.class);
                intent.putExtra("USER_NAME",userName);
                startActivity(intent);
            }
        });
        more_page_row3 = findViewById(R.id.more_page_row3);
        more_page_row3.setOnClickListener(v -> {
            if ("".equals(userName)){
                showRegistrationAlertDialog();
            }else {
                Intent intent = new Intent(SettingsActivity.this, UsageRecordsActivity.class);
                intent.putExtra("USER_NAME",userName);
                startActivity(intent);
            }
        });
        more_page_row4 = findViewById(R.id.more_page_row4);
        more_page_row4.setOnClickListener(v -> {
            Intent intent = new Intent(SettingsActivity.this, ClientSettings.class);
            intent.putExtra("USER_NAME",userName);
            startActivity(intent);
        });

        more_page_row5 = findViewById(R.id.more_page_row5);
        more_page_row5.setOnClickListener(v -> {
            if ("".equals(userName)){
                showRegistrationAlertDialog();
            }else {
                Intent intent = new Intent(SettingsActivity.this, FeedBackActivity.class);
                intent.putExtra("USER_NAME",userName);
                startActivity(intent);
            }
        });
        more_page_row6 = findViewById(R.id.more_page_row6);
        more_page_row6.setOnClickListener(v -> {
            if (currentToast != null) {
                currentToast.cancel();
            }

            currentToast = Toast.makeText(this, "It is currently the latest version ^_^", Toast.LENGTH_SHORT);
            currentToast.show();
        });
        more_page_row7 = findViewById(R.id.more_page_row7);
        more_page_row7.setOnClickListener(v -> {
            Intent intent = new Intent(SettingsActivity.this, AboutActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        });

        tvPrivacyPolicy = findViewById(R.id.tv_privacy_policy);

        tvTermsConditions = findViewById(R.id.tv_terms_conditions);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            tvPrivacyPolicy.setText(Html.fromHtml(privacyPolicyText, Html.FROM_HTML_MODE_LEGACY));
            tvTermsConditions.setText(Html.fromHtml(termsConditionsText, Html.FROM_HTML_MODE_LEGACY));
        } else {
            tvPrivacyPolicy.setText(Html.fromHtml(privacyPolicyText));
            tvTermsConditions.setText(Html.fromHtml(termsConditionsText));
        }


        tvPrivacyPolicy.setOnClickListener(v -> {
            showPrivacyPolicy();
        });

        tvTermsConditions.setOnClickListener(v -> {
            showTermsConditions();
        });

    }

    private void showRegistrationAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Login is required")
                .setMessage("You don't have an account, please login first.")
                .setPositiveButton("Login", (dialog, which) -> {
                    dialog.dismiss();
                    Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);
                    startActivity(intent);
                    AppManager.finishAll();
                }).setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss()).setCancelable(false)
                .show();
    }

    private void showPrivacyPolicy() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_privacy_policy, null);

        TextView textView = dialogView.findViewById(R.id.textview_privacy_policy);

        String fullText = getString(R.string.privacy_policy_content);
        String googlePlayServices = getString(R.string.google_play_services);
        String chatRobot = getString(R.string.chat_robot);

        SpannableString ss = new SpannableString(fullText);
        int gpsStart = fullText.indexOf(googlePlayServices);
        int gpsEnd = gpsStart + googlePlayServices.length();
        ss.setSpan(new URLSpan("https://www.google.com/policies/privacy/"), gpsStart, gpsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.BLUE), gpsStart, gpsEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new UnderlineSpan(), gpsStart, gpsEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        int crStart = fullText.indexOf(chatRobot);
        int crEnd = crStart + chatRobot.length();
        ss.setSpan(new URLSpan("https://sites.google.com/view/chat-robot-privacy"), crStart, crEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.BLUE), crStart, crEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new UnderlineSpan(), crStart, crEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        textView.setText(ss);
        textView.setTextSize(16);
        textView.setMovementMethod(LinkMovementMethod.getInstance());

        builder.setView(dialogView);

        builder.setPositiveButton("Close", (dialog, id) -> dialog.dismiss());
        builder.setCancelable(false);

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void showTermsConditions() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_terms_conditions, null);
        TextView textView = dialogView.findViewById(R.id.textview_terms_condition);

        String fullText = getString(R.string.terms_conditions);
        String googlePlayServices = getString(R.string.google_play_services);
        String chatRobot = getString(R.string.chat_robot);

        SpannableString ss = new SpannableString(fullText);
        int gpsStart = fullText.indexOf(googlePlayServices);
        int gpsEnd = gpsStart + googlePlayServices.length();
        ss.setSpan(new URLSpan("https://www.google.com/policies/privacy/"), gpsStart, gpsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.BLUE), gpsStart, gpsEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new UnderlineSpan(), gpsStart, gpsEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        int crStart = fullText.indexOf(chatRobot);
        int crEnd = crStart + chatRobot.length();
        ss.setSpan(new URLSpan("https://sites.google.com/view/chat-robot-privacy"), crStart, crEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.BLUE), crStart, crEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new UnderlineSpan(), crStart, crEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        textView.setText(ss);
        textView.setTextSize(16);
        textView.setMovementMethod(LinkMovementMethod.getInstance());

        builder.setView(dialogView);

        builder.setPositiveButton("Close", (dialog, id) -> dialog.dismiss());
        builder.setCancelable(false);

        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
